package com.lz.sports.adapter

import android.view.View
import android.view.ViewGroup
import com.lz.library.adapter.BindingAdapter
import com.lz.library.adapter.BindingViewHolder
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.bean.DeviceInfo
import com.lz.sports.databinding.ItemDeviceGroupEditBinding
import com.lz.sports.widget.ShakeClickListener

//设备分组编辑
class DeviceGroupEditAdapter : BindingAdapter<DeviceInfo>() {

    var itemMenuListener: OnItemMenuListener? = null

    override fun getLayoutResId(viewType: Int): Int = R.layout.item_device_group_edit

    override fun getVariableId(viewType: Int): Int = BR.device

    override fun onCreateItem(parent: ViewGroup, viewType: Int): BindingViewHolder {
        val vh = super.onCreateItem(parent, viewType)
        val listener: View.OnClickListener = object : ShakeClickListener() {
            override fun shakeClick(v: View) {
                val position = vh.bindingAdapterPosition
                if (position < 0) return
                when (v.id) {
                    R.id.tv_edit -> itemMenuListener?.onItemEdit(position)
                    R.id.tv_unbind -> itemMenuListener?.onItemUnbind(position)
                }
            }
        }
        val binding = vh.getBinding<ItemDeviceGroupEditBinding>()
        binding.tvEdit.setOnClickListener(listener)
        binding.tvUnbind.setOnClickListener(listener)
        return vh
    }

    //item中的项点击
    interface OnItemMenuListener {

        //编辑序号
        fun onItemEdit(position: Int)

        //取消绑定
        fun onItemUnbind(position: Int)
    }
}